﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Model;
using MedicLibrary.Controler;

namespace MedicLibrary.View
{
    public partial class frmDSHangSX : Form
    {
        public String action;

        public delegate void getHangSX(int ID, String Name, String Country);

        public getHangSX getHSX;

        public frmDSHangSX()
        {
            InitializeComponent();
        }

        private void frmDSNhomThuoc_Load(object sender, EventArgs e)
        {
            dgvDSHangSX.DataSource = HangSX.getDanhSachHangSX("");
        }

        private void dgvDSNhomThuoc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.action == "LIST")
            {
                Session.IDHangSX = Int32.Parse(dgvDSHangSX.SelectedRows[0].Cells["ID"].Value.ToString());
                frmChiTietHangSX frmCTHSX = new frmChiTietHangSX();
                frmCTHSX.applyAction("VIEW");
                frmCTHSX.Show();
            }
            else if (this.action == "SEARCH")
            {

                getHSX(Int32.Parse(dgvDSHangSX.SelectedRows[0].Cells["ID"].Value.ToString()), dgvDSHangSX.SelectedRows[0].Cells["TenHangSX"].Value.ToString(), dgvDSHangSX.SelectedRows[0].Cells["QuocGia"].Value.ToString());
                this.Close();
            }
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmChiTietHangSX ctNhom = new frmChiTietHangSX();
            ctNhom.applyAction("ADD");
            ctNhom.ShowDialog();
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Session.IDHangSX = Int32.Parse(dgvDSHangSX.SelectedRows[0].Cells["ID"].Value.ToString());
            frmChiTietHangSX ctNhom = new frmChiTietHangSX();
            ctNhom.applyAction("EDIT");
            ctNhom.ShowDialog();
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            HangSX nt = new HangSX();
            nt.ID = Int32.Parse(dgvDSHangSX.SelectedRows[0].Cells["ID"].Value.ToString());
            nt.deleteHangSX();

            dgvDSHangSX.DataSource = HangSX.getDanhSachHangSX("");
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            dgvDSHangSX.DataSource = HangSX.getDanhSachHangSX(txtTimKiem.Text);
        }

        private void btnXemHet_Click(object sender, EventArgs e)
        {
            dgvDSHangSX.DataSource = HangSX.getDanhSachHangSX("");
            txtTimKiem.Text = "";
        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "LIST")
            {
                status = true;
            }
            else if (this.action == "SEARCH")
            {
                status = false;
            }
            this.btnThem.Enabled = status;
            this.btnXoa.Enabled = status;
            this.btnSua.Enabled = status;
        }

        private void txtTimKiem_KeyUp(object sender, KeyEventArgs e)
        {
            dgvDSHangSX.DataSource = HangSX.getDanhSachHangSX(txtTimKiem.Text);
        }
    }
}
